package bookpos.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import bookpos.entity.Book;
import bookpos.entity.BookType;
import bookpos.util.core.dao.HibernateDao;
import bookpos.vo.BookVO;
@Repository
public class BookDao extends HibernateDao<Book> {

	
	public List<BookVO> findAllBook(int page, int rows, String sort, String order) {
		Criteria criteria = getCurrentSession().createCriteria(Book.class);
		criteria.setFirstResult((page-1) * rows);
		criteria.setMaxResults(rows);
		if ("asc".equals(order)) {
			criteria.addOrder(Order.asc(sort));
		} else {
			criteria.addOrder(Order.desc(sort));
		}
		return criteria.list();
	}

	public boolean addInventory(Integer id, Integer quantity) {
		updateQuantity(id,"+"+quantity);
		return true;
	}
	
	public boolean subInventory(Integer id, Integer quantity) {
		updateQuantity(id,"-"+quantity);
		return true;
	}

	public void updateQuantity(Integer id, String quantity) {
		   String sql = "update bookpos_book set quantity=quantity"+quantity+" where id="+id;
		   doUpdateBySQL(sql);
	}
	
}
